package com.example.bookstore.entity

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonView
import javax.persistence.*

/**
 * Created by IntelliJ IDEA.
 * User: lvshe
 * Date: 2019/8/10
 * Time: 14:45
 */
@Table
@Entity
class BookEntity : BaseEntity {

    constructor() : super()
    constructor(name: String, author: String, price: Double, regularPrice: Double, imageUrl: String, desc: String, detailImageUrl: String) : super() {
        this.name = name
        this.author = author
        this.price = price
        this.regularPrice = regularPrice
        this.imageUrl = imageUrl
        this.description = desc
        this.detailImageUrl = detailImageUrl
    }

    @JsonView(BookView::class)
    @Column(nullable = false)
    var name: String = ""

    @JsonView(BookView::class)
    @Column(nullable = false)
    var author: String = ""

    @JsonView(BookView::class)
    @Column(nullable = false)
    var price: Double = 0.0

    @JsonView(BookView::class)
    @Column(nullable = false)
    var regularPrice: Double = 0.0

    @JsonView(BookView::class)
    @Column(nullable = false)
    var imageUrl: String = ""

    @JsonView(BookView::class)
    @Lob
    @Column(nullable = false)
    var description: String = ""

    @JsonView(BookView::class)
    @Column(nullable = false)
    var detailImageUrl: String = ""


//    @Fetch(FetchMode.JOIN)
    @JsonView(BookWithCategory::class)
    @ManyToMany(mappedBy = "books")
    @JsonIgnoreProperties("books")
    var categories: Set<BookCategoryEntity> = hashSetOf()

    @ManyToOne
//    @Fetch(FetchMode.JOIN)
    @JoinColumn(name = "press_id", nullable = true, updatable = false)
    @JsonIgnoreProperties("books")
    var press: PressEntity? = null

    interface BookView : BaseWithIdView
    interface BookWithCategory : BookView, BookCategoryEntity.BookCategoryView
}